﻿#region Copyright © 2011 Prageeth Silva <prageeth@thenewcoders.com>
/*
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the author(s) be held liable for any damages arising from
 * the use of this software.
 * 
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 * 
 *   1. The origin of this software must not be misrepresented; you must not
 *      claim that you wrote the original software. If you use this software
 *      in a product, an acknowledgment in the product documentation would be
 *      appreciated but is not required.
 * 
 *   2. Altered source versions must be plainly marked as such, and must not
 *      be misrepresented as being the original software.
 * 
 *   3. This notice may not be removed or altered from any source distribution.
 */
#endregion

namespace SourcePlus.Core
{

    public class FileItem
    {

        public static int MaxNameLength { get; set; }

        static FileItem()
        {
            MaxNameLength = 70;
        }

        public string Directory { get; protected set; }

        public string FileName { get; protected set; }

        public string ShortName { get; protected set; }

        private string longName = string.Empty;
        public string LongName 
        {
            get { return this.longName; }
            set
            {
                this.longName = value;
                this.FileName = this.ObtainFileName(value);
                this.ShortName = this.ObtainShortName(value);
                this.Directory = this.ObtainDIrectory(value);
            }
        }

        public FileItem(string fullname)
        {
            this.LongName = fullname;
        }

        private string ObtainShortName(string name)
        {

            if (name == null) { return null; }

            if (name.Length <= MaxNameLength) { return name; }
            
            if (name.Length == this.FileName.Length) { return name; }

            string filename = this.FileName;
            if (filename.Length > MaxNameLength + 4)
            {
                int i = (MaxNameLength / 3);
                filename = filename.Substring(0, i-1) + "..." + filename.Substring(filename.Length - i);
            }

            return name.Substring(0, MaxNameLength - filename.Length - 4)
                    + "...\\" + filename;
        }

        private string ObtainFileName(string name)
        {
            if (name == null) { return null; }
            return name.Substring(name.LastIndexOf('\\') + 1);
        }

        private string ObtainDIrectory(string name)
        {
            if (name == null) { return null; }
            return name.Substring(0, name.LastIndexOf('\\') + 1);
        }

        public override string ToString()
        {
            return this.ShortName;
        }

    }

    public class SourceFile : FileItem
    {

        public string RtfContent { get; set; }

        public bool IsValid { get; set; }

        public SourceFile(string fullname)
            : base(fullname)
        {
            this.RtfContent = null;
            this.IsValid = false;
        }
        
    }

    public class DestinationFile : FileItem
    {

        public DestinationFile(string fullname)
            : base(fullname)
        {

        }

    }

}
